package de.derhess.projects.pkr.controller
{	
	import de.derhess.projects.pkr.models.CourtCost;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.events.PropertyChangeEvent;

	/**
	 * released under MIT License (X11)
	 * http://www.opensource.org/licenses/mit-license.php
	 * 
	 * Class  ControllerCourtCost
	 * 
	 * @author Florian Weil [derhess.de, Deutschland]
	 * @see http://blog.derhess.de
	 */
	public class ControllerCourtCost
	{
		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		private var model:CourtCost;
		
		//--------------------------------------------------------------------------
		//
		//  Initialization
		//
		//--------------------------------------------------------------------------
		public function ControllerCourtCost(data:CourtCost)
		{
			model = data;
						
			this.model.instance1.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewSum);
			this.model.appeal1.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewSum);
			this.model.appeal2.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewSum);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		
		//--------------------------------------------------------------------------
		//
		//  Additional getters and setters
		//
		//--------------------------------------------------------------------------
		public function setCourtFee(value:Number):void
		{
			this.model.instance1.baseValue = value;
			this.model.appeal1.baseValue = value;
			this.model.appeal2.baseValue = value;
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		public function toString():String 
		{
			return "[ControllerCourtCost] : model: " + model.toString();	
		}
		//--------------------------------------------------------------------------
		//
		//  API
		//
		//--------------------------------------------------------------------------
		public function setStartValues(courtFeeValue:Number):void
		{
			this.model.instance1.factor = 1;
			this.model.instance1.baseValue = courtFeeValue;
			
			this.model.appeal1.factor = 0;
			this.model.appeal1.baseValue = courtFeeValue;
			
			this.model.appeal2.factor = 0;
			this.model.appeal2.baseValue = courtFeeValue;
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Eventhandling
		//
		//--------------------------------------------------------------------------
		private function handleNewSum(e:PropertyChangeEvent):void
		{
			this.model.sum = model.instance1.sum + model.appeal1.sum + model.appeal2.sum;
		}
		//--------------------------------------------------------------------------
		//
		//  Broadcasting
		//
		//--------------------------------------------------------------------------
		
		
	}
}